<template>
  <div class="app-container">
    <div v-if="$permission('institutionalInquiries:page')">
      <!-- 条件栏 -->
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList">
          <el-button v-show="true" @click="print">打印</el-button>
          <el-button v-show="true" @click="exportExcel"
            >导出excel</el-button
          > </NewPageFilter
        >、
      </div>
      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="getInstitutionalInquiries"
        :tab-index="true"
        :check-box="false"
        v-model:query="filterInfo.query.queryMap"
        :field-list="tableInfo.fieldList"
        @handleEvent="handleEvent"
        @pageInfo="pageInfo"
      ></TableContainer>
    </div>

    <PageDialog
      v-if="dialogTipsInfo.visible"
      :title="dialogTipsInfo.title"
      v-model:visible="dialogTipsInfo.visible"
      :width="dialogTipsInfo.width"
      :bt-loading="dialogTipsInfo.btLoading"
      :bt-list="dialogTipsInfo.btList"
    >
      <div class="asset-msg">
        <div class="asset-msg-label">
          <i class="el-icon-warning"></i>{{ dialogTipsInfo.msg }}：
        </div>
        <el-radio-group v-model="dialogTipsInfo.flag">
          <el-radio :label="true">当前页数据</el-radio>
          <el-radio :label="false">全部页数据</el-radio>
        </el-radio-group>
      </div>
    </PageDialog>
  </div>
</template>

<script setup name="institutionalInquiries">
import {
  getInstitutionalInquiries,
  exportInstitutionalInquiries,
  printInstitutionalInquiries,
} from '@/api/15189.js'

import { fatchTemplateByCode } from '@/api/base'
const { proxy } = getCurrentInstance()
const page = ref({
  page: true,
  curPage: 1, // 当前页
  pageSize: 20, // 每页条数
})
const dialogTipsInfo = ref({
  visible: false,
  title: '提示',
  btLoading: false,
  type: '',
  msg: '',
  width: '400px',
  flag: true,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '打印',
      type: 'primary',
      event: checkConfirm,
      show: true,
    },
  ],
})
const filterInfo = ref({
  sortArr: [],
  copyQueryMap: {},
  query: {
    sortString: '',
    groupMap: {},
    groups: [],
    queryMap: {
      // depotAuth: {
      //   queryType: "IN",
      //   asTbName: "depot_stock",
      //   columnName: "depot_id",
      //   queryCategory: "AND",
      //   value: "depotAuth"
      // }
    },
    customizeUserVO: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
  btnList: [
    {
      type: 'button',
      label: '打印',
      event: print,
      show: proxy.$permission('institutionalInquiries:print'),
    },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('institutionalInquiries:execl'),
    },
  ],
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: '机构代码', value: 'code' },
    { label: '机构名称', value: 'name' },
    { label: '联系人', value: 'contact' },
    { label: '联系电话', value: 'phone' },
    { label: '传真', value: 'fax' },
    { label: '邮箱', value: 'mail' },
    { label: '邮编', value: 'zipCode' },
    { label: '详细地址', value: 'receiveAddressValue' },
  ],
})
const clearType = ref(true)
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      break
  }
}
function exportExcelList() {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  exportInstitutionalInquiries(params).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}

function printList() {
  fatchTemplateByCode({
    resourceCode: 'institutionalInquiries:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let params = {
    ...filterInfo.value.query.queryMap,
    ...page.value,
    page: dialogTipsInfo.value.flag,
  }
  printInstitutionalInquiries(params).then((resp) => {
    if (resp && resp.success) {
      let data = proxy.$fn.deepClone(resp.data)
      proxy.$lodop.preview(temp, data)
    }
  })
}
function exportExcel() {
  //导出
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'excel'
  dialogTipsInfo.value.msg = '请选择数据导出范围'
  dialogTipsInfo.value.btList[1].label = '导出'
  dialogTipsInfo.value.flag = true
}
function print() {
  //打印
  dialogTipsInfo.value.visible = true
  dialogTipsInfo.value.type = 'print'
  dialogTipsInfo.value.msg = '请选择当数据打印范围'
  dialogTipsInfo.value.btList[1].label = '打印'
  dialogTipsInfo.value.flag = true
}
//打印导出
function pageInfo(obj) {
  page.value = obj
}
function checkConfirm() {
  if (dialogTipsInfo.value.type === 'excel') {
    exportExcelList()
  } else if (dialogTipsInfo.value.type === 'print') {
    printList()
  }
  dialogTipsInfo.value.visible = false
  dialogTipsInfo.value.msg = ''
}
function handleClose() {
  dialogTipsInfo.value.visible = false
}

let obj = {
  nameLike: {
    label: '机构名称',
    value: 'nameLike',
    type: 'column',
    componentType: 'input',
    event: search,
    hidden: false,
    show: true,
  },
  codeLike: {
    label: '机构编码',
    value: 'codeLike',
    type: 'column',
    componentType: 'input',
    event: search,
    hidden: false,
    show: true,
  },
}
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(obj),
})

onMounted(() => {
  getList()
})
</script>

<style scoped></style>
